using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.FileManipulation.Properties;

namespace Workflows.Components.FileManipulation
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class FileComponent:WorkflowComponentBase
    {
        private FileInstruction _Instruction;
        private FileOutput _Output;


        /// <summary></summary>
        public FileComponent():base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.File_Tasks.ToString();
            this.Description = "simple file manipulation";
            this.ComponentIcon = Resources.notepad;
            this.ComponentImage = Resources.Book;
            this.ComponentName = "File task";

            this._Instruction = new FileInstruction(this.ComponentID,Guid.Empty,string.Empty,string.Empty,FileActionType.Move);
            this._Output=new FileOutput(this.ComponentID,Guid.Empty,string.Empty);
        }

        #region props
        public override InstructionBase Instruction
        {
            get { return _Instruction; }
            set { _Instruction =(FileInstruction) value; }
        }

        public override ResultBase Output
        {
            get { return _Output; }
            set { _Output =(FileOutput) value; }
        }

        /// <summary></summary>
        public string SrcPath
        {
            get { return this._Instruction.SrcPath; }
            set { this._Instruction.SrcPath = value; }
        }

        /// <summary></summary>
        public string TgtPath
        {
            get { return this._Instruction.TgtPath; }
            set { this._Instruction.TgtPath = value; }
        }

        /// <summary></summary>
        public FileActionType ActionType
        {
            get { return this._Instruction.ActionType; }
            set { this._Instruction.ActionType = value; }
        }

        /// <summary></summary>
        public bool OverWrite
        {
            get
            {
                return this._Instruction.OverWrite;
            }
            set
            {
                this._Instruction.OverWrite = value;
            }
        }
        #endregion

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SourcePath", this.SrcPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TargetPath", this.TgtPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileActionType", ((int) this.ActionType).ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc,xNode,"OverWrite",this.OverWrite.ToString());

            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FileComponent fileComponent = base.Instantiate(xNode) as FileComponent;

                fileComponent.SrcPath = xNode.Attributes.GetNamedItem("SourcePath").Value;
                fileComponent.TgtPath = xNode.Attributes.GetNamedItem("TargetPath").Value;
                fileComponent.ActionType =
                    (FileActionType) int.Parse(xNode.Attributes.GetNamedItem("FileActionType").Value);
                fileComponent.OverWrite = bool.Parse(xNode.Attributes.GetNamedItem("OverWrite").Value);

                return fileComponent;
            }
            else
            {
                throw new Exception("Invalid type to instantiate");
            }
        }
    }
}
